import { defineStore } from 'pinia';
import { computed, ref } from 'vue';
import $api from '@/api/index';

export const useProductStore = defineStore('productStore', () => {

  // 所有选择的分类id收集
  const categoryIdData = ref({
    id1: '',
    id2: '',
    id3: '',
  })

  // 一级分类数据和请求函数
  const category1Data = ref({})
  const reqCategory1 = async function () {
    const result = await $api.product.attr.reqCategory1()
    if (result?.data?.code === 200) {
      Object.assign(category1Data.value, result.data.data)
      return 'ok'
    } else {
      return Promise.reject(result?.data?.message)
    }
  }

  // 二级分类数据和请求函数
  const category2Data = ref({})
  const reqCategory2 = async function () {
    const id = categoryIdData.value.id1
    const result = await $api.product.attr.reqCategory2(id)
    if (result?.data?.code === 200) {
      Object.assign(category2Data.value, result.data.data)
      return 'ok'
    } else {
      return Promise.reject(result?.data?.message)
    }
  }

  // 三级分类数据和请求函数
  const category3Data = ref({})
  const reqCategory3 = async function () {
    const id = categoryIdData.value.id2
    const result = await $api.product.attr.reqCategory3(id)
    if (result?.data?.code === 200) {
      Object.assign(category3Data.value, result.data.data)
      return 'ok'
    } else {
      return Promise.reject(result?.data?.message)
    }
  }

  // spu编辑状态
  // 0: infoTable   1:spuEditor   2:skuEditor
  const spuEditor = ref(0)

  // 编辑spu数据收集表单
  const spuFormData = ref({
    category3Id: 0,
    spuName: '',
    description: '',
    tmId: null,
    spuImageList: [],
    spuSaleAttrList: []
  })
  // 打开spu编辑
  const openSpuEditor = function (row) {
    spuFormData.value = {
      category3Id: 0,
      spuName: '',
      description: '',
      tmId: null,
      spuImageList: [],
      spuSaleAttrList: []
    }
    spuEditor.value = 1
    if (row) {
      Object.assign(spuFormData.value, row)
    }
  }
  // 关闭spu编辑
  const closeSpuEditor = function () {
    spuEditor.value = 0
  }
  // 保存spu编辑
  const saveSpuEditor = async function (data) {
    let result = await $api.product.spu.reqSaveSpuInfo(data)
    if (result?.data?.code === 200) {

      closeSpuEditor()

      return 'ok'
    } else {
      return Promise.reject(result?.data?.message)
    }
  }



  // 编辑spu数据收集表单
  const skuFormData = ref({
    // category3Id: "",
    spuID: '',
    tmId: '',
    skuName: '', // 名称
    price: '',  // 价格
    weight: '', // 重量
    skuDesc: '',  // 描述
    isSale: 0,
    skuDefaultImg: '',  //sku图片地址
    skuAttrValueList: [
      {
        attrId: '', // 平台属性的id
        valueId: '',  // 属性的id
      }
    ],
    skuImageList: [],
    skuSaleAttrValueList: [
      {
        saleAttrId: '', // 属性id
        saleAttrValueId: '',  // 属性值的id
      }
    ]
  })
  // 打开sku编辑
  const openSkuEditor = function (row) {
    skuFormData.value = {
      // category3Id: "",
      spuID: '',
      tmId: '',
      skuName: '', // 名称
      price: '',  // 价格
      weight: '', // 重量
      skuDesc: '',  // 描述
      isSale: 0,
      skuDefaultImg: '',  //sku图片地址
      skuAttrValueList: [],
      skuImageList: [],
      skuSaleAttrValueList: []
    }
    spuEditor.value = 2
    skuFormData.value.spuID = row.id
    skuFormData.value.tmId = row.tmId
    skuFormData.value.category3Id = row.category3Id
  }
  // 打开sku编辑
  const closeSkuEditor = function () {
    spuEditor.value = 0
  }

  return {
    categoryIdData, category1Data, category2Data, category3Data, spuEditor, spuFormData, skuFormData,
    reqCategory1, reqCategory2, reqCategory3, openSpuEditor, closeSpuEditor, saveSpuEditor, openSkuEditor, closeSkuEditor
  }
})